/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: LoggingEventLevelFilter.java 189 2007-01-27 12:34:36Z jack.rybicki $
 */
package logmatcher.filters;

import logmatcher.Configurable;
import logmatcher.InitializationException;
import logmatcher.InputFilter;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
 * Only messages with specified levels may pass.
 *
 * @version $Revision: 189 $
*/
public class LoggingEventLevelFilter implements InputFilter, Configurable {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(LoggingEventLevelFilter.class);

    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** Allowed levels. */
    private Set<Level> levels = new HashSet<Level>(1);

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
         * Creates a new LoggingEventLevelFilter object.
         */
    public LoggingEventLevelFilter() {
        super();
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * @see logmatcher.InputFilter#accept(java.lang.Object)
     */
    public boolean accept(Object event) {
        if (levels.contains(Level.ALL)) {
            return true;
        }

        if (levels.contains(Level.OFF)) {
            return false;
        }

        if (event instanceof LoggingEvent) {
            return levels.contains(((LoggingEvent)event).getLevel());
        }

        return false;
    }


    /**
     * Read the object properties from configuration source.
     *
     * @see logmatcher.Configurable#configure(org.apache.commons.configuration.Configuration, java.lang.String)
     */
    public void configure(Configuration config, String id)
        throws InitializationException {
        String[] lev = config.getStringArray(id + ".levels");

        if (lev.length == 0) {
            throw new InitializationException("Key not found: " + id + "levels");
        }

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Config levels: ");

            for (int i = 0; i < lev.length; i++) {
                LOGGER.debug(lev[i]);
            }
        }

        for (int i = 0; i < lev.length; i++) {
            levels.add(Level.toLevel(lev[i].trim()));
        }

        if (LOGGER.isDebugEnabled()) {
            for (Iterator it = levels.iterator(); it.hasNext();) {
                LOGGER.debug("Categorized: " + it.next());
            }
        }
    }
}
